package org.eclipse.emf.index.omds.ui.control;

import java.io.File;
import java.io.ObjectInputStream.GetField;
import java.util.HashMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.index.omds.ui.content.OMSResourceObject;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jst.jee.ui.internal.navigator.OpenJEEResourceAction;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.ide.IDE;
import org.eclipse.wst.common.componentcore.resources.IVirtualComponent;

public class ResourceLoader extends OpenJEEResourceAction{
	
	private OMSResourceObject omsResource = null;
	private StructuredSelection selection = null;
	
	public void openResource(Object c){
		if(c instanceof StructuredSelection){
			selection = (StructuredSelection)c;
			omsResource = (OMSResourceObject) selection.getFirstElement();
			IWorkspace workspace = ResourcesPlugin.getWorkspace();
			IProject myProject = workspace.getRoot().getProject(omsResource.getProjectName());
			//Find the corresponding folder name by analysing the package name.
			String tempPackageName = omsResource.getPackageName();
			int index = tempPackageName.lastIndexOf('.');
			String packageName = tempPackageName.substring(0, index);
			String folderName = packageName.replace('.', '\\');
			IFolder myFolder = myProject.getFolder("src\\"+folderName);
			IFile myFile = null;
			myFile = myFolder.getFile(omsResource.getResourceName()+".java");
			String fileName = myFile.getLocation().toString();
			
			IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();			
		    HashMap map = new HashMap();
		    map.put(IMarker.LINE_NUMBER, new Integer(5));
		    map.put(IWorkbenchPage.EDITOR_ID_ATTR, 
		    	"org.eclipse.ui.DefaultTextEditor");
		    IMarker marker = null;
			try {
				marker = myFile.createMarker(IMarker.TEXT);
			} catch (CoreException e) {
				System.err.println("The marker could not be created.");
			}
		    try {
				marker.setAttributes(map);
			} catch (CoreException e) {
				System.err.println("The marker was not set with attribues.");
			}
//			page.openEditor(marker); //2.1 API
			try {
				IDE.openEditor(page,myFile);  //3.0 API
			} catch (PartInitException e) {
				System.err.println("The editor cannot be opened in the editor.");
			} 
			try {
				marker.delete();
			} catch (CoreException e) {
				System.err.println("The marker could not be deleted.");
			}
		}
		

	}
	

}
